//
//  NIMAvatarImageView+ZLCustom.m
//  ZLYoYoIM_Example
//
//  Created by 赵磊 on 2021/1/19.
//  Copyright © 2021 itzhaolei. All rights reserved.
//

#import "NIMAvatarImageView+ZLCustom.h"
#import <NIMKit/NIMKit.h>
#import <NIMKit/NIMKitInfoFetchOption.h>
#import <NIMKit/NIMKitUrlManager.h>
#import <NIMKit/UIImage+NIMKit.h>
#import "UIImage+ZLIMCustom.h"

@implementation NIMAvatarImageView (ZLCustom)

- (void)setAvatarBySession:(NIMSession *)session
{
    NIMKitInfo *info = nil;
    if (session.sessionType == NIMSessionTypeTeam) {
        info = [[NIMKit sharedKit] infoByTeam:session.sessionId option:nil];
    } else if (session.sessionType == NIMSessionTypeSuperTeam) {
        info = [[NIMKit sharedKit] infoBySuperTeam:session.sessionId option:nil];
    } else {
        NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
        option.session = session;
        info = [[NIMKit sharedKit] infoByUser:session.sessionId option:option];
    }
    [self setImageWithUrlString:info.avatarUrlString placeholderImage:info.avatarImage options:0];
}

- (void)setAvatarByMessage:(NIMMessage *)message
{
    NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
    option.message = message;
    NSString *from = message.from;
    NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:from option:option];
    [self setImageWithUrlString:info.avatarUrlString placeholderImage:info.avatarImage options:0];
}

- (void)setImageWithUrlString:(NSString *)urlString placeholderImage:(UIImage *)placeholderImage options:(SDWebImageOptions)options {
    NSString *safeUrlString = nil;
    if (urlString) {
        safeUrlString = [urlString componentsSeparatedByString:@"?"].firstObject;
        if ([safeUrlString rangeOfString:@"https"].location != NSNotFound) {
            safeUrlString = [safeUrlString stringByReplacingOccurrencesOfString:@"https" withString:@"http"];
        }
    }
    UIImageView *imageView = (UIImageView *)[self valueForKey:@"imageView"];
    imageView.layer.cornerRadius = 5.0;
    imageView.layer.masksToBounds = YES;
    __weak typeof(imageView)weakImageView = imageView;
    if (!placeholderImage) {
        placeholderImage = [UIImage nim_imageInKit:@"avatar_user"];
    }
    imageView.image = nil;
    __weak typeof(self)weakSelf = self;
    [imageView sd_setImageWithURL:[NSURL URLWithString:safeUrlString] placeholderImage:placeholderImage options:0 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        weakImageView.image = image;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            UIImageView *imageView = (UIImageView *)[weakSelf valueForKey:@"imageView"];
            imageView.image = image;
        });
    }];
}

///根据名称设置一个图片
- (void)setImageWithName:(NSString *)imageName {
    __weak typeof(self)weakSelf = self;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        UIImageView *imageView = (UIImageView *)[weakSelf valueForKey:@"imageView"];
        UIImage *image = [UIImage imageNamedInIM:imageName];
        imageView.image = image;
    });
    
}

@end
